package com.zhy.generator.commons.exception;

import cn.hutool.core.exceptions.ValidateException;
import com.zhy.generator.commons.response.ResultResponse;
import com.zhy.generator.commons.response.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public ResultResponse exceptionHandler(HttpServletRequest request, Exception e) {
        //全局异常处理逻辑
        log.error("全局异常处理逻辑错误{}", e.getMessage());
        e.printStackTrace();
//        if ( e instanceof BindException || e instanceof MethodArgumentNotValidException){
//            return ResultUtil.jsonMsg(1,"参数验证失败，请传入正确的参数",null);
//        }
        if(e instanceof BindException){
            List<ObjectError> allErrors = ((BindException) e).getAllErrors();
            if (allErrors.size() > 0) {
                ObjectError error = allErrors.get(0);
                String defaultMessage = error.getDefaultMessage();
                return ResultUtil.jsonMsg(1, defaultMessage, null);
            }
        }
        return ResultUtil.jsonMsg(1, e.getMessage()==null? "" : e.getMessage(), null);
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultResponse methodArgumentNotValidException(HttpServletRequest request, MethodArgumentNotValidException e) {
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        if (allErrors.size() > 0) {
            ObjectError error = allErrors.get(0);
            String defaultMessage = error.getDefaultMessage();
            return ResultUtil.jsonMsg(1, defaultMessage, null);
        }

        return ResultUtil.jsonMsg(1, e.getMessage(), null);
    }

    /**
     * controller异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = ControllerLayerException.class)
    public ResultResponse controllerLayerExceptionHandler(HttpServletRequest request, Exception e) {
        log.error("controller层爆异常了{}", e.getMessage());
        return ResultUtil.jsonMsg(1, e.getMessage(), null);
    }

    /**
     * services异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = ServiceLayerException.class)
    public ResultResponse serviceLayerExceptionHandler(HttpServletRequest request, Exception e) {
        log.error("services层爆异常了{}", e.getMessage());
        return ResultUtil.jsonMsg(1, e.getMessage(), null);
    }

    /**
     * 安全校验，如果没有传就报错了
     *
     * @return
     */
    @ExceptionHandler(value = {NoHandlerFoundException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ResultResponse noHandlerFoundException(NoHandlerFoundException exception) {
        log.error("安全校验，如果没有传就报错了{}", exception.getMessage());
        return ResultUtil.jsonMsg(1, "Bad Request", null);
    }

    /**
     * 验证异常处理啊 hutool的异常处理
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(value = ValidateException.class)
    @ResponseBody
    public ResultResponse handleServiceException(ValidateException exception) {
        log.error("验证异常处理啊 hutool的异常处理{}", exception.getMessage());
        return ResultUtil.jsonMsg(1, exception.getMessage(), null);
    }
}
